/**
 *  Author:Greg Bugaj
 *  Desc:Provide some extended functionality for an array
 *  Nothing fancy but it does simplifyes working with arrays
 *  Added new methods to the ArrayObject to confirm to ECMA-262, 3rd specs for array
 *  http://www.webreference.com/programming/javascript/ncz/column4/
 *  http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
 */
//Make sure that the both arrays have this same data in them
if(Array.prototype.compareTo==null){
    Array.prototype.compareTo=function(thatArray){
        var thisArray=this;
        if(thisArray.length!=thatArray.length){
            return false;
        }
        for(var i=0;i<thisArray.length;i++){
            if(thisArray[i]!=thatArray[i]){
                return false;
            }
        }
        return true;
    }
}
//Checks if the array contains given value and it is of this same type ===
if(Array.prototype.contains==null){
    Array.prototype.contains=function(value){
        var thisArray=this;
        for(var i=0;i<thisArray.length;i++){
            if(thisArray[i]==value){
                return true;
            }
        }
        return false;
    }
}


//Provides indexOf functionality for an array
if(Array.prototype.indexOf==null){}

Array.prototype.indexOf=function(value, startIndex){
    var pos=0;
    var len = this.length;
    if(startIndex){
         pos = startIndex;
    }
    var thisArray=this;
    for(var i=pos;i<len;i++){
        if(thisArray[i]==value){
            return i;
        }
    }
    return -1;
 }

/**
 * returns the index of the given item's last occurrence.
 * @param {Object} value
 */
Array.prototype.lastIndexOf=function(value){
    var thisArray=this;
    for(var i=thisArray.length; i>0; i--){
        if(thisArray[i]==value){
            return i;
        }
    }
    return -1;
}


/**
 * runs a function on every item in the array and returns true if the function returns true for every item.
 * @param {Object} callback
 */
Array.prototype.every=function(callback){

}
/**
 *                           callback (element, index, array) { return true-false}
 * @param {Object} callback
 */
if (!Array.prototype.filter){}

  Array.prototype.filter = function( fun )
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this, res))
          res.push(val);
      }
    }
    return res;
  };


if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

/**
 * Find unique elements of the array
 */
Array.prototype.unique = function()
{
    var len = this.length;
    var res = new Array();
    var val = this; // in case fun mutates this
     for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
         if(!res.contains(val[i])){
             res[res.length]=val[i];
         }
      }
    }
     return res;
}
